package app.tool;

import java.io.File;
import java.util.HashSet;
import java.util.Set;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import org.rygh.core.file.FileIterator;
import org.rygh.core.file.FileSaver;

import app.App;

public class prepareImdb extends App {

	static boolean isMovie(String val) {
		return val.startsWith("im");
	}

	static boolean isPerson(String val) {
		return val.startsWith("ip");
	}

	public static void main(String[] args) {
		init();
		run();
		bye();
	}

	static void run() {
		File path = new File("/home/nanmus/Experiments/db/bak/imdb");
		File file = new File(path, "imdb_relation_id_id.txt");

		FileSaver writer = new FileSaver(new File(path, "imdb.ttl"));
		writer.begin();

		String str, line;
		String prefix = "http://clem.org/imdb/";
		String type = "http://www.w3.org/1999/02/22-rdf-syntax-ns#type";
		String movie, person;

		movie = "http://clem.org/imdb/wordnet_movie_106613686";
		person = "http://clem.org/imdb/wordnet_person_100007846";

		FileIterator iter = new FileIterator(file);
		iter.begin();

		int counter = 0;

		while ((line = iter.next()) != null) {
			counter++;

			if (line.trim().isEmpty())
				continue;

			String[] vals = line.split("\\s+");
			assert vals.length == 3 : line;

			str = String.format("<%s%s> <%s%s> <%s%s>.\n", prefix, vals[1],
					prefix, vals[0], prefix, vals[2]);
			writer.write(str);
		}

		iter.end();
		System.out.printf("processed %d lines.\n", counter);

		//

		Set<String> movies = new HashSet<>();
		Set<String> persons = new HashSet<>();

		file = new File(path, "imdb_id_property_literal.txt");

		Pattern pat = Pattern.compile("\\s+");
		String[] vals = new String[3];
		String subj;

		int a, b;

		iter = new FileIterator(file);
		iter.begin();

		while ((line = iter.next()) != null) {
			counter++;

			if (line.trim().isEmpty())
				continue;

			Matcher m = pat.matcher(line.trim());

			a = 0;
			assert m.find();
			b = m.start();
			subj = vals[0] = line.substring(a, b);

			if (isMovie(subj))
				movies.add(subj);
			else if (isPerson(subj))
				persons.add(subj);
			else
				assert false;

			a = m.end();
			assert m.find();
			b = m.start();
			vals[1] = line.substring(a, b);
			vals[2] = line.substring(m.end()).trim();

			vals[2] = vals[2].replaceAll("\\\\", "");
			vals[2] = vals[2].replaceAll("\\\"", "\\\\\"");

			str = String.format("<%s%s> <%s%s> \"%s\".\n", prefix, vals[0],
					prefix, vals[1], vals[2]);

			writer.write(str);
		}

		iter.end();
		System.out.printf("processed %d lines.\n", counter);

		//

		System.out.printf("%d movies, %d persons.\n", movies.size(),
				persons.size());

		for (String s : movies) {
			str = String.format("<%s%s> <%s> <%s>.\n", prefix, s, type, movie);
			writer.write(str);
		}

		for (String s : persons) {
			str = String.format("<%s%s> <%s> <%s>.\n", prefix, s, type, person);
			writer.write(str);
		}

		writer.end();
	}
}
